<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

/* No direct access to this file */
defined('_JEXEC') or die('Restricted access');

/* import Joomla modelitem library */
jimport('joomla.application.component.modelitem');

/* class bgModelbg */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        /* Get pagination request variables */
        $limit = $mainframe->getUserStateFromRequest('com_questionpage.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        /* In case limit has been changed, adjust it */
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    /* get all records questionpage */
	function getAll($list = null){

        $qr = "SELECT DISTINCT * FROM `pagq` LEFT JOIN `serq` on `pagq`.`PAGQ_SERQ_CODE` = `serq`.`SERQ_CODE`  ";
        $where = array();
       /* search */
       
       if(isset($list['filter_search']) && $list['filter_search']){
            $where[] = "PAGQ_TITLE LIKE '%$list[filter_search]%'";
       } 
       
       if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }
       /* get total number record */
       $this->_total = $this->getTotal($where);
       
       $qr .= $where;
       /* order by */
       $qr .= " ORDER BY PAGQ_CODE DESC"; 
       
       /* pagination */
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
       /* load data */

       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
        
       return $result;
       
	}

    /* get total records */
    function getTotal($where = null){
        /* Load the content if it doesn't already exist */
        if ($this->_total === null) {
            $query = "SELECT count(*) FROM `pagq` LEFT JOIN `serq` on `pagq`.`PAGQ_SERQ_CODE` = `serq`.`SERQ_CODE` $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        return $this->_total;
    } 
    
    function getPagination(){
        /* Load the content if it doesn't already exist */
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    /* get code, name questionare */
    function getQuestionare(){
        $qr = "SELECT SERQ_CODE, SERQ_TITLE  FROM serq";
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObjectList();
        
        return $result;
    }
    
    /* check ID exist */
    public function checkExistID($cid){
        $qr = "SELECT PAGQ_CODE FROM pagq WHERE PAGQ_CODE = $cid";
        /* load data */
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();
       
        if(!empty($result)){ 
            return true;
        }
        return false;
    }
    
    /* count number questions in page question */
    public function countPageQuestions($id) {
        $query = "SELECT count(*) FROM ques WHERE QUES_PAGQ_CODE = $id";
        $this->_db->setQuery($query);
        $this->_total = $this->_db->loadResult();
        
        return $this->_total;
    }
    
    /* load all records with id */
    public function load($cid){
        $query = "SELECT * FROM pagq WHERE PAGQ_CODE=($cid)";
        
        $this->_db->setQuery($query);
        $result = $this->_db->loadObject();
     
        return $result;
    }
    
     /* save record questionpage */
     public function save($data, $addNew){
        /* add new questionpage */
        if($addNew == true)
        {
            /* insert values to pagq table */
            $qr = "INSERT INTO pagq (PAGQ_CODE, PAGQ_SERQ_CODE, PAGQ_TITLE, PAGQ_DESCRIPTION, PAGQ_IMG, PAGQ_PUBLISHED) 
            VALUES ('$data[PAGQ_CODE]', '$data[PAGQ_SERQ_CODE]', '$data[PAGQ_TITLE]', '$data[PAGQ_DESCRIPTION]', '$data[PAGQ_IMG]'
             , '$data[PAGQ_PUBLISHED]')";
        
            $this->_db->setQuery($qr);
            if($this->_db->query())
                return true;
            else
                return false;
        /* update questionpage */ 
        } else {
            /* update valuse to pagq table */
            $qr = "UPDATE pagq SET PAGQ_SERQ_CODE = '$data[PAGQ_SERQ_CODE]', PAGQ_TITLE = '$data[PAGQ_TITLE]',
            PAGQ_DESCRIPTION = '$data[PAGQ_DESCRIPTION]', PAGQ_IMG = '$data[PAGQ_IMG]', PAGQ_PUBLISHED = '$data[PAGQ_PUBLISHED]' 
            WHERE PAGQ_CODE = '$data[PAGQ_CODE]'";
            
            $this->_db->setQuery($qr);
            if($this->_db->query())
                return true;
            else
                return false;
        }
        
     }
     
    /* remove pagq items */
    public function remove($cid){
        $cid = implode(',', $cid);
    
        $query = "DELETE FROM pagq WHERE PAGQ_CODE IN($cid)";
  
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    /**
     * Remove QUES from PAGE_CODE 
    */
    public function removeques($cid){
        $cid = implode(',', $cid);
    
        $query = "DELETE FROM ques WHERE QUES_PAGQ_CODE IN($cid)";
  
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    /**
     * Remove ANSQ from QUES_CODE 
    */
    public function removeansq($cid){
        $cid = implode(',', $cid);
    
        $query = "DELETE FROM ansq WHERE ANSQ_QUES_CODE IN($cid)";
  
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    /**
     * Load question from PAGQ_CODE 
    */
    public function loadQuesFromPagq($cid){
        
        $query = "SELECT * FROM ques WHERE QUES_PAGQ_CODE = $cid";
        
        $this->_db->setQuery($query);
        $result = $this->_db->loadObjectList();
        
        return $result;
    }
    /**
     * GET IMAGE FROM PAGQ_CODE 
    **/
    public function getImgFromPAGQ_CODE($cid){
        $qr = "SELECT PAGQ_IMG FROM pagq WHERE PAGQ_CODE = $cid";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
       return $result;
    }
}
